const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    // 所有文件的输出路径
    path: path.resolve(__dirname, "dist"), // 绝对路径
    filename: "js/main.js",
    publicPath: "/",
    // 自动清空上次打包内容
    // 原理：打包前，清空path目录
    clean: true,
  },
  module: {
    rules: [
      {
        test: /\.css$/i, // 只检测以.css结尾的文件
        use: ["style-loader", "css-loader"], // 执行顺序：从右到左，或从上到下
        // css-loader: 将css资源编译成commonjs的模块到js中
        // style-loader: 将js中的css通过创建style标签添加到html文件中
      },
      {
        test: /\.less$/i,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      {
        test: /\.(png|jpe?g|gif|webp)$/i,
        type: "asset",
        generator: {
          filename: "image/[hash:10][ext][query]",
        },
      },
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          sources: true,
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "public/index.html"),
    }),
  ],
  devServer: {
    /* static: {
      directory: path.join(__dirname, "public"),
      serveIndex: true, // 启用目录列表功能
      watch: true, // 启用文件系统监听
    }, */
    host: "localhost",
    port: "3000",
    open: true,
    hot: true,
    watchFiles: ["public/index.html"],
    // compress: true,
  },
  mode: "development",
};
